<?php

class avatar_model extends CI_Model {

	var $gallery_path;
	var $gallery_path_url;
	var $avatar_path;
	
	function __construct()
	{
		parent::__construct();
		
		$this->gallery_path = realpath(APPPATH . '../images');
		$this->gallery_path_url = base_url().'images/';
	}

	function is_avatar_exits($avatar_path)
	{
		$this->db->from('user');
		$this->db->where('userid', $this->session->userdata('userid'));
		$query=$this->db->get();
		$profile=$query->result();
		return file_exists($avatar_path);
	}
	function do_upload()
	{
		/*if($this->is_avatar_exits())
		 {
		unlink($this->avatar_path);
		}*/
		$config = array(
				'allowed_types' => 'jpg|jpeg|gif|png',
				'upload_path' => $this->gallery_path,
				'max_size' => 100000
		);

		$this->load->library('upload', $config);
		$this->upload->do_upload();
		$image_data = $this->upload->data();
		
		$this->db->from('user');
		$this->db->where('userid', $this->session->userdata('userid'));
		$profile['avatar_name']=$image_data['file_name'];
		$this->db->update('user', $profile);
		
		$config = array(
				'source_image' => $image_data['full_path'],
				'new_image' =>$this->gallery_path . '/thumbs',
				'maintain_ration' => true,
				'width' => 150,
				'height' => 100
		);

		$this->load->library('image_lib', $config);
		$this->image_lib->resize();
		redirect('user_controller/edit_profile');
	}

	/*function get_images() {

	$files = scandir($this->gallery_path);
	$files = array_diff($files, array('.', '..', 'thumbs'));

	$images = array();

	foreach ($files as $file) {
	$images []= array (
			'url' => $this->gallery_path_url. $file,
			'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file
	);
	}

	return $images;
	}*/

}
?>